#!/bin/sh

#
# linuxrc file for initrd (Initial Ram Disk) for LTSP
#
# Copyright (C) 2001 James A. McQuillan <jam@McQuil.Com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# 23/11/2007 - Poy -  Modificacion para Munix
#
# 04/19/2003 -jam- Changed from dhclient to dhcpcd, to work better with w2k
#                  dhcp servers.  This change is thanks to
#                  Paul Whittaker <paw@si1.dod.gov.au>
#
# 04/11/2003 -jam- Added DPORT as a kernel commandline arg, to cause the
#                  dhclient program to use an alternate port. Default is 67.
#
# 03/04/2002 -jam- Changed to use modprobe instead of insmod, so that the module
#                  dependencies would work properly.
# 02/28/2002 -jam- Added IRQ as a kernel cmdline arg, to be passed to the
#                  NIC module.  Also made IO so that it would always be passed
#                  to the NIC module, even if not required by that module.
# 12/11/2001 -jam- Added loading of 8390.o when using e2100, smc-ultra and wd
# 10/18/2001 -jam- Added support for Linux Progress Patch (LPP
# 09/22/2001 -jam- Added auto-probing to determine what type of NIC
# 09/18/2001 -jam- Added loading of the 8390.o module when either
#                  3c503, hp-plus, hp, ne or ne2k-pci are used.
#
# 09/02/2001 -jam- Initial writing of this script
#

echo "==============================================================================="
echo "Running /linuxrc"

echo "Mounting /proc"
mount -t proc /proc /proc

[ "${INITRD_DBG}" = "1" ] && exec /bin/sh

[ -w /proc/progress ] && echo 40 "Detecting network card" >/proc/progress
[ "${SLEEP}" -gt 0 ] && sleep ${SLEEP}

if [ -z "${NIC}" ]; then
    NIC=`/bin/pci_scan /etc/niclist`
    if [ -z "${NIC}" ]; then
        [ -w /proc/progress ] && echo f >/proc/progress
        echo
        echo "ERROR!  Could not automatically detect the network card."
        echo "        PCI cards should be detected automatically."
        echo "        ISA cards cannot be detected, so they require"
        echo "        the nic driver to be specified in a 'NIC=' parameter"
        echo "        to be passed on the kernel command line,"
        echo "        usually specified in option-129, in the"
        echo "        /etc/dhcpd.conf file.  See the LTSP docs for more info."
        echo
        exit 1
    fi
fi

[ "${INITRD_DBG}" = "2" ] && exec /bin/sh

IOADDR=""

#
# See if we need to pass an IO argument to the driver
#
[ "${INITRD_DBG}" = "3" ] && exec /bin/sh
case ${NIC} in
    3c503 | hp_plus | hp | ne | wd)
        if [ -z "${IO}" ]; then
            [ -w /proc/progress ] && echo f >/proc/progress
            echo
            echo "ERROR!  No IO specified."
            echo "        The NIC requires an IO=0xNNN entry on the kernel command line."
            echo "        This is setup in the /etc/dhcpd.conf file using option-129"
            echo
            exit 1
        fi
        ;;
esac

if [ -n "${IO}" ]; then
    IOADDR="io=${IO}"
fi

if [ -n "${IRQ}" ]; then
    IRQOPT="irq=${IRQ}"
fi

[ -w /proc/progress ] && echo 45 "Loading network driver" >/proc/progress
[ "${SLEEP}" -gt 0 ] && sleep ${SLEEP}

[ "${INITRD_DBG}" = "4" ] && exec /bin/sh
echo "linuxrc: Installing ${NIC} driver"
echo "modprobe ${NIC} ${IOADDR} ${IRQOPT} ${NICOPT}"

modprobe -v ${NIC} ${IOADDR} ${IRQOPT} ${NICOPT}
if [ $? -ne 0 ]; then
    [ -w /proc/progress ] && echo f >/proc/progress
    echo
    echo "ERROR!  Failed to install the NIC driver module!"
    echo "        This could be caused by the wrong module for your particular"
    echo "        network interface card.  Double check the 'option-129' entry"
    echo "        in your /etc/dhcpd.conf file."
    echo
    exit 1
fi
 
#
# dhcpcd runs as a daemon.  Once it acquires the info from the
# server, it will automatically configure the interface.  A script called
# /etc/dhcpc/dhcpcd.exe is used to perform additional configuration.
#
[ "${INITRD_DBG}" = "6" ] && exec /bin/sh
[ -w /proc/progress ] && echo 50 "Sending DHCP request" >/proc/progress
[ "${SLEEP}" -gt 0 ] && sleep ${SLEEP}

if [ -z "${DPORT}" ]; then
    DPORT=67
fi

echo "Running dhcpcd on port ${DPORT}"
/bin/dhcpcd -p ${DPORT} eth0 >/tmp/dhcpcd.out 2>&1
if [ $? -ne 0 ]; then
    [ -w /proc/progress ] && echo f >/proc/progress
    echo
    cat /tmp/dhcpcd.out
    echo
    echo "ERROR! dhcpcd failed!"
    echo
    exit 1
fi

#
# The dhcpcd program deposited a bunch of important
# information in the /etc/dhcpc/dhcpcd-eth0.info file.
# Sourcing the file will set a bunch of environment variables.
#

# 
# On a slow machine, the dhclient program might fork and return
# control to the /linuxrc script, before the info file gets written.
# 

COUNT=5
while [ $COUNT -gt 0 -a ! -f /etc/dhcpc/dhcpcd-eth0.info ]; do
    sleep 1
    COUNT=`expr $COUNT - 1`
done

if [ ! -f /etc/dhcpc/dhcpcd-eth0.info ]; then
    [ -w /proc/progress ] && echo f >/proc/progress
    echo
    echo "ERROR!  No dhcpcd-eth0.info file."
    echo "        This usually indicates that dhcpcd did not get"
    echo "        a response from the DHCP server, or the NIC driver"
    echo "        isn't working properly with the network card"
    echo
    exit 1
fi

. /etc/dhcpc/dhcpcd-eth0.info

#
# Check for a root-path from the dhcp server
#
[ "${INITRD_DBG}" = "7" ] && exec /bin/sh
if [ -z "${ROOTPATH}" ]; then
    [ -w /proc/progress ] && echo f >/proc/progress
    echo
    echo "ERROR!  No root-path.  Check your DHCP configuration, to make"
    echo "        sure that the 'option root-path' is specified"
    echo
    exit 1
fi

NFS_IP=` echo ${ROOTPATH} | cut -d : -f 1`
NFS_DIR=`echo ${ROOTPATH} | cut -d : -f 2`

if [ "${NFS_IP}" = "${NFS_DIR}" ]; then
    #
    # Only the root directory is specified, then we have to use the
    # default IP address.  For now, we'll use the DHCP server address
    #
    NFS_IP=${DHCPSERVER}
fi

[ -w /proc/progress ] && echo 55 "Mounting root filesystem" >/proc/progress
[ "${SLEEP}" -gt 0 ] && sleep ${SLEEP}

[ -z "${MOPTS}" ] && MOPTS="nolock,ro"

#########################################################################
#		Agregado para ProRC
#########################################################################
echo
echo
echo
echo "=================================================================="
echo " "
echo "  	PRotocolo de Reparto de Carga PRoRC "
echo " "
SERVER_ASIGN=`/bin/socketclient $NFS_IP 1703 10`
ERRSAL=$?

if [ $ERRSAL -eq 0 ]; then
NFS_IP_ORI="$NFS_IP"
NFS_IP="$SERVER_ASIGN"
echo "Booteo de $NFS_IP_ORI "
echo "Servidor de Aplicaciones Designado SApD Ok: ${SERVER_ASIGN}."
echo " "
fi

echo "Mounting root filesystem: ${NFS_DIR} from: ${NFS_IP}"
mount -n -o ${MOPTS} ${NFS_IP}:${NFS_DIR} /mnt

if [ $? -ne 0 ]; then
    echo "ERROR!  no se pudo montar del Servidor Designado por Reparto de Carga $NFS_IP !"
    echo "ERROR!  Continua por defecto  con $NFS_IP_ORI !"
    sleep 1
    NFS_IP="$NFS_IP_ORI"
    mount -n -o ${MOPTS} ${NFS_IP}:${NFS_DIR} /mnt
    
    if [ $? -ne 0 ]; then
	[ -w /proc/progress ] && echo f >/proc/progress
	echo
	echo "ERROR!  Failed to mount the root directory via NFS!"
	echo "        Possible reasons include:"
	echo
	echo "        1) NFS services may not be running on the server"
	echo "        2) Workstation IP does not map to a hostname, either"
	echo "           in /etc/hosts, or in DNS"
	echo "        3) Wrong address for NFS server in the DHCP config file"
	echo "        4) Wrong pathname for root directory in the DHCP config file"
	echo
	exit 1
    fi
fi

echo " "
echo "Continua Booteo desde : $NFS_IP ."
echo " "
echo "=================================================================="
echo
echo
echo
cat /etc/dhcpc/dhcpcd-eth0.info | grep -v "DHCPSID" > /etc/dhcpc/dhcpcd-eth0.val 2> /dev/null
cat /etc/dhcpc/dhcpcd-eth0.val > /etc/dhcpc/dhcpcd-eth0.info 2> /dev/null
echo "DHCPSID=$NFS_IP" >> /etc/dhcpc/dhcpcd-eth0.info 2> /dev/null
########################################################################
#	Fin Agregado para ProRC
########################################################################


[ "${INITRD_DBG}" = "8" ] && exec /bin/sh
[ -w /proc/progress ] && echo 60 "Pivoting root" >/proc/progress
[ "${SLEEP}" -gt 0 ] && sleep ${SLEEP}

umount /proc
echo "Doing the pivot_root"
cd /mnt
/sbin/pivot_root . oldroot
cd /

/usr/bin/[ "${INITRD_DBG}" = "9" ] && exec /bin/sh
echo "Mounting the devfs filesystem"
mount -n -t devfs /devfs /dev

#
# We temporarily re-mount /proc, so we can display the progress message
#
mount -t proc /proc /proc
/usr/bin/[ -w /proc/progress ] && echo 65 "Running /sbin/init" >/oldroot/proc/progress
/usr/bin/[ "${SLEEP}" -gt 0 ] && sleep ${SLEEP}

umount /proc

/usr/bin/[ "${INITRD_DBG}" = "10" ] && exec /bin/sh
echo "Running /sbin/init"
exec /sbin/init 
